import urllib.request as urr
from bs4 import UnicodeDammit as UD
from bs4 import BeautifulSoup as BS
import threading
def Imgsp(url):
    global thre
    global coun
    try:
        urls = []
        req = urr.Request(url, headers=ua)
        data = urr.urlopen(req)
        data = data.read()
        dam = UD(data, ["UTF-8", "GBK"])
        data = dam.unicode_markup
        sou = BS(data, "lxml")
        imag = sou.select("img")
        for i in imag:
            try:
                src = i["src"]
                purl = urr.urljoin(url, src)
                if purl not in urls:
                    coun = coun + 1
                    T = threading.Thread(target=download, args=(purl,coun))
                    T.setDaemon(False)
                    T.start()
                    thre.append(T)
            except Exception as err:
                print(err)
    except Exception as err:
        print(err)
def download(url, coun):
    try:
        if (url[len(url)-4] == "."):
            ext = url[len(url)-4:]
        else:
            ext = ".png"
        req = urr.Request(url, headers=ua)
        data = urr.urlopen(req, timeout=100)
        data = data.read()
        file = open("imga\\" + str(coun) + ext, "wb")
        file.write(data)
        file.close()
        print( str(coun) + ext)
    except Exception as err:
        print(err)

sta_url = "https://movie.douban.com/"

ua = {
    "User-Agent":"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36"
}
if __name__ == '__main__':
    coun = 0
    thre =[]
    Imgsp(sta_url)

# print(data.read())